* Calculate distance to default

clear all

* Set your directory path here
local root "D:\Understanding_Bank_Payouts"

local finalfile_yearly "`root'\Data\Final\Finalfile_yearly.dta" 

use `finalfile_yearly', clear

keep instkey year avg_rwcapitalratioWY avg_roaaWY
sum avg_rwcapitalratioWY avg_roaaWY, detail

generate helper = avg_roaaWY/100
drop avg_roaaWY
rename helper avg_roaaWY

generate helper = avg_rwcapitalratioWY/100
drop avg_rwcapitalratioWY
rename helper avg_rwcapitalratioWY

generate roa_plus_car = avg_roaaWY + avg_rwcapitalratioWY

drop if year > 2006		// Only until 2006

collapse (mean) avg_roa_plus_car = roa_plus_car (sd) sd_roa = avg_roaaWY, by(instkey)

generate disttodef2006 = ln(avg_roa_plus_car/sd_roa)
label variable disttodef2006 "Distance to default, estimated at year-end 2006"

save "Distance_to_default_2006.dta", replace

exit, clear
